﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
   public class DynamicDAL
    {
       SqlConnection str = new SqlConnection("Data Source=PC201609040948;Initial Catalog=TianTianDai_Db;Persist Security Info=True;User ID=sa;pwd=123456");
       /// <summary>
       /// 显示行业动态
       /// </summary>
       /// <returns></returns>
       public List<Dynamics> ShowDynamic()
       {
           str.Open();
           string sql = "select * from Dynamics";
           SqlDataAdapter adap = new SqlDataAdapter(sql, str);
           DataTable ta = new DataTable();
           adap.Fill(ta);
           str.Close();
           List<Dynamics> listdynamic = new List<Dynamics>();
           foreach (DataRow item in ta.Rows)
           {
               Dynamics d = new Dynamics();
               d.DynamicContent = item["DynamicContent"].ToString();
               d.DynamicId = Convert.ToInt32(item["DynamicId"]);
               d.DynamicName = item["DynamicName"].ToString();
               d.DynamicTime = Convert.ToDateTime(item["DynamicTime"]);
               d.Issuer = item["Issuer"].ToString();
               listdynamic.Add(d);
           }
           return listdynamic;
       }
       /// <summary>
       /// 删除信息
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public int DeleteDynamic(int id)
       {
           str.Open();
           string sql = "delete Project where Dynamics='" + id + "'";
           SqlCommand com = new SqlCommand(sql, str);
           var i = com.ExecuteNonQuery();
           return i;
       }
       /// <summary>
       /// 添加信息
       /// </summary>
       /// <param name="p"></param>
       /// <returns></returns>
       public int Dynamic_Add(Model.Dynamics p)
       {
           str.Open();
           string sql = "insert into Project values('" + p.DynamicContent + "','" + p.DynamicId + "','"+p.DynamicName+"','"+p.DynamicTime+"','"+p.Issuer+"')";
           SqlCommand com = new SqlCommand(sql, str);
           var i = com.ExecuteNonQuery();
           return i;
       }
       /// <summary>
       /// 显示详细信息
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public Dynamics ShowDynamicsX(int id)
       {
           str.Open();
           string sql = "select * from Dynamics where DynamicId='" + id + "'";
           SqlDataAdapter adap = new SqlDataAdapter(sql, str);
           DataTable ta = new DataTable();
           adap.Fill(ta);
           str.Close();
           List<Dynamics> listDynamics = new List<Dynamics>();
           foreach (DataRow item in ta.Rows)
           {
               Dynamics j = new Dynamics();
               j.DynamicContent = item["DynamicContent"].ToString();
               j.DynamicId = Convert.ToInt32(item["DynamicId"]);
               j.DynamicName = item["DynamicName"].ToString();
               j.DynamicTime = Convert.ToDateTime(item["DynamicTime"]);
               j.Issuer = item["Issuer"].ToString();
               listDynamics.Add(j);
           }
           return listDynamics.FirstOrDefault(); 
       }
       /// <summary>
       /// 修改信息
       /// </summary>
       /// <param name="data"></param>
       /// <returns></returns>
       public int UpdateDynamics(Dynamics data)
       {
           str.Open();
           string sql = "update Dynamics set  DynamicContent='" + data.DynamicContent + "',DynamicName='" + data.DynamicName + "',DynamicTime='" + data.DynamicTime + "',Issuer='" + data.Issuer + "' where DynamicId='" + data.DynamicId + "'";
           SqlCommand com = new SqlCommand(sql, str);
           int i = com.ExecuteNonQuery();
           return i;
       }
       /// <summary>
       /// 搜索
       /// </summary>
       /// <param name="pp"></param>
       /// <returns></returns>
       public List<Dynamics> SeleteDynamics(string name)
       {
           str.Open();
           string sql = "selete DynamicId,DynamicContent,DynamicName,DynamicTime,Issuer from Dynamics where DynamicName like '" + name + "'";
           SqlDataAdapter adap = new SqlDataAdapter(sql, str);
           DataTable ta = new DataTable();
           adap.Fill(ta);
           List<Dynamics> listProject = new List<Dynamics>();
           foreach (DataRow item in ta.Rows)
           {
               Dynamics d = new Dynamics();
               d.DynamicContent = item["DynamicContent"].ToString();
               d.DynamicId = Convert.ToInt32(item["DynamicId"]);
               d.DynamicName = item["DynamicName"].ToString();
               d.DynamicTime = Convert.ToDateTime(item["DynamicTime"]);
               d.Issuer = item["Issuer"].ToString();
               listProject.Add(d);
           }
           return listProject;
       }
    }
}
